package com.taofuxn.yycreazy.adpater.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

/**
 * Created by wanghuafu on 16/9/16.
 */
public class TaofuRecyclerViewHodler extends RecyclerView.ViewHolder {

    private SparseArray<View> views;
    private Context mContext;
    private View convertView;

    public TaofuRecyclerViewHodler(Context mcContext, View itemView) {
        super(itemView);
        this.convertView = itemView;
        this.mContext = mcContext;
        this.views = new SparseArray<>();
    }

    /**
     * 对view进行缓存
     * @param viewId
     * @param <T>
     * @return
     */
    public  <T extends View> T retrieveView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = convertView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 设置文本
     * @param viewId
     * @param text
     * @return
     */
    public TaofuRecyclerViewHodler setText(int viewId, String text) {
        TextView view = retrieveView(viewId);
        if (!TextUtils.isEmpty(text))
            view.setText(text);
        return this;
    }

}
